/*
 * controls.h
 *
 *  Created on: Dec 17, 2011
 */

#include "entities/structs.h"
#include "utils/util.h"

#ifndef CONTROLS_H_
#define CONTROLS_H_

const float PI = (float)atan(1.0)*4;

CameraAngle* camera;
int last_key_pressed = -1;
// angle of rotation for the camera direction
//float angle = 0.0f;
//position of the MainCell
//float Cx=0.0f, Cy = 0.0f,Cz=0.0f;
//position of the Camera
//float dirX,dirY,dirZ;
//float angleY,angleX,angleZ;
// the key states. These variables will be zero
//when no key is being presses
//float deltaAngle = 0.0f;
//float deltaMove = 0;
//int xOrigin;
//int yOrigin;


void cleanup(vector<Cell*> &cells, Octree* octree) {
	for(unsigned int i = 0; i < cells.size(); i++) {
		delete cells[i];
	}
	delete _octree;
}

float camera_pos_z(Cell * mainCell){
	return mainCell->pos[2] + (mainCell->r * 2) + 6;
}
void handleKeypress(unsigned char key, int x, int y, vector<Cell*> &cells, Octree* octree, Cell *mainCell ) {
	//	int mod = glutGetModifiers();
	switch (key) {
	case 27: //Escape key
		cleanup(cells, octree);
		exit(0);
	case 106: // j
		set_cell_x(mainCell, false, 0.2f);
		return;
	case 10: // CTRL+j
		return;
	case 105: // i
		set_cell_z(mainCell, false, 0.2f);

		return;
	case 9: // CTRL+i
		set_cell_y(mainCell, true, 0.2f);

		return;
	case 107: // k
		set_cell_z(mainCell, true, 0.2f);

		return;
	case 11: // CTRL+k
		set_cell_y(mainCell, false, 0.2f);

		return;
	case 108: // L
		set_cell_x(mainCell, true, 0.2f);

		return;
	case 12: // CTRL+L
		return;
	case 23: // CTRL+W

		return;
	case 1: // CTRL+A
		//camera_rotate_z(true, 0.5f);

		return;
	case 19: // CTRL+S
		//camera_depth(false, 0.5f);

		return;
	case 119: //W
		//Point3d ligth1(c1.x()+3*sin(tau1),1,c1.z()+3*cos(tau1));
		//Point3d ligth2(-3,c2.y()+7*sin(tau2),c2.z()+7*cos(tau2));
		camera->pos_y += 3.0f;
		break;
	case 115: //S
		camera->pos_y -= 3.0f;
		//camera->pos_z = camera_pos_z(mainCell) * cos(camera->tau_z);
		return;
	case 4: // CTRL+D
//		camera_rotate_z(false, 0.5f);

		return;
	case 97: // A
//		camera_rotate_y(false, 0.5f);

		return;
	case 100: // D
//		camera_rotate_y(true, 0.5f);

		return;
	case ' ':
		//addCells(50,1.5f);
		return;
	}
}


void processSpecialKeys(int key, int x, int y, Cell* mainCell, CameraAngle* camera) {

	//Vec3f direction = (mainCell->pos - Vec3f(dirX,dirY,dirZ))*0.05f;

	switch(key) {
	case GLUT_KEY_UP:
//		mainCell->pos.set_x(mainCell->pos.get_x() + direction.get_x());
//		mainCell->pos.set_y(mainCell->pos.get_y() + direction.get_y());
//		mainCell->pos.set_z(mainCell->pos.get_z() + direction.get_z());

		break;
	case GLUT_KEY_DOWN:
//		mainCell->pos.set_x(mainCell->pos.get_x() - direction.get_x());
//		mainCell->pos.set_y(mainCell->pos.get_y() - direction.get_y());
//		mainCell->pos.set_z(mainCell->pos.get_z() - direction.get_z());

		break;
	case GLUT_KEY_LEFT:
		camera->pos_x -= 3.0f;
		break;
	case GLUT_KEY_RIGHT:

		camera->pos_x += 3.0f;
		break;
	}
}



/***
 * Handler of mouse movement
 */

void mouseButton(int button, int state, int x, int y, Cell *mainCell) {

	// only start motion if the left button is pressed
	if (button == GLUT_LEFT_BUTTON) {

		// when the button is released
		if (state == GLUT_UP) {
			//angle += deltaAngle;
//			xOrigin = -1;
//			yOrigin = -1;
		}
		else  {// state = GLUT_DOWN
//			xOrigin = mainCell->pos.get_x();
//			yOrigin = mainCell->pos.get_y();
		}
	}
}

/***
 * Detect the movement of the mouse
 * then check in which direction the mouse camera must rotate x,y,z
 */

void mouseMove(int x, int y) {
//
//	//right rotate
//	if(xOrigin > x){
//		angleZ = angleY += 0.05f;
//		xOrigin = x;
//	}
//
//	//left rotate
//	if(xOrigin < x){
//		angleZ = angleY -= 0.05f;
//		xOrigin = x;
//	}
//
//	//    //up rotate
//	if(yOrigin < y){
//		angleZ = angleX -= 0.05f;
//		if(angleX < -1.5)
//			angleZ =-angleZ;
//
//		yOrigin = y;
//	}
//
//	else if(yOrigin > y){
//		angleZ = angleX += 0.05f;
//		if(angleX > 1.5)
//			angleZ =-angleZ;
//		yOrigin = y;
//	}

}

//void set_maincell_position(Cell *mainCell){
//	Cx = mainCell->pos.get_x();
//	Cy = mainCell->pos.get_y();
//	Cz = mainCell->pos.get_z();
//
//	dirX = Cx+mainCell->r*10*sin(angleY);
//	dirY = Cy+mainCell->r*10*sin(angleX);
//	dirZ = Cz+mainCell->r*10*cos(angleZ);
//}


#endif /* CONTROLS_H_ */
